package views.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import views.util.GameColors;
import core.constants.Constants;

public class SvegliaTalpeView extends GameView implements ActionListener {

	private JPanel main;
	private GridBagConstraints gbc = new GridBagConstraints();
	private JButton[][] tableView;
	private ActionListener currentActionListener;
	private static final int HEIGHT = 10, WIDTH = 10;
	private Timer cronometro = new Timer(1000, this);
	private int timeMax = Constants.getGameTime();
	private boolean goAhead = true;

	public SvegliaTalpeView(int[][] table) {
		super(GameColors.ARANCIONE, GameColors.ARANCIONE_CHIARO, "TEMPO",
				"PUNTEGGIO: 0", "INVIO PER INIZIARE");
		gbc.insets = new Insets(1, 1, 1, 1);
		main = new JPanel(new GridBagLayout());
		main.setBackground(GameColors.ARANCIONE);
		main.setPreferredSize(new Dimension(800, 680));
		initializeTable(table);
		add(main, BorderLayout.CENTER);

	}

	@Override
	public void checkFocusMain() {
		main.requestFocus();
	}

	public void disableAll() {
		for (int i = 0; i < tableView.length; i++)
			for (int j = 0; j < tableView.length; j++)
				tableView[i][j].setEnabled(false);
	}

	public void enableAll() {
		for (int i = 0; i < tableView.length; i++)
			for (int j = 0; j < tableView.length; j++)
				tableView[i][j].setEnabled(true);
	}

	/**
	 * Metodo chiamato ogni volta che termina uno schema di gioco e aggiorna il
	 * valore dei JButton, senza ricrearli, con il valore della nuova table
	 * passata
	 * 
	 * @param table
	 */
	public void refresh(int[][] table) {
		Color status = null;
		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 0; j < WIDTH; j++) {

				switch (table[i][j]) {
				case 0:
					tableView[i][j].setIcon(new ImageIcon());
					tableView[i][j].setBackground(Color.WHITE);
					break;
				case 1:
					tableView[i][j].setIcon(sleepingMole);
					break;
				default:
					tableView[i][j].setIcon(awakeMole);
				}
			}
		}
	}

	private ImageIcon sleepingMole = new ImageIcon(
			"resources\\svegliaTalpa\\talpaAddormentata.png");
	private ImageIcon awakeMole = new ImageIcon(
			"resources\\svegliaTalpa\\talpaSveglia.png");
	private Dimension BUTTON_DIMENSION = new Dimension(45, 45);

	/**
	 * Metodo invocato solo una volta dal costruttore che inizializza i JButton
	 * e assegna loro il valore delle celle di table
	 * 
	 * @param table
	 */
	private void initializeTable(int[][] table) {
		tableView = new JButton[HEIGHT][WIDTH];
		// Color status = null;
		gbc.gridx = 0;
		gbc.gridy = 0;
		JButton emptyButton = new JButton();
		emptyButton.setPreferredSize(BUTTON_DIMENSION);
		emptyButton.setBackground(GameColors.ARANCIONE);
		emptyButton.setBorderPainted(false);
		emptyButton.setEnabled(false);
		main.add(emptyButton, gbc);
		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 1; j < WIDTH + 1; j++) {
				gbc.gridx = i;
				gbc.gridy = j;
				tableView[i][j - 1] = new JButton();
				tableView[i][j - 1].setName(i + "-" + (j - 1));
				switch (table[i][j - 1]) {
				case 0:
					tableView[i][j - 1].setBackground(Color.WHITE);
					break;
				case 1:
					tableView[i][j - 1].setIcon(sleepingMole);
					break;
				default:
					tableView[i][j - 1].setIcon(awakeMole);
				}
				tableView[i][j - 1].setPreferredSize(BUTTON_DIMENSION);
				tableView[i][j - 1].setBackground(Color.WHITE);
				// tableView[i][j].setBackground(status);
				// System.out.println(tableView[i][j].getText());
				main.add(tableView[i][j - 1], gbc);

			}
		}

	}

	@Override
	public void gameEnded() {
		other.setVisible(false);
		// score.setVisible(false);
		gameClock.setVisible(false);
		// tab.setVisible(false);
		main.removeAll();
		disableAll();
		gbc.insets = new Insets(0, 0, 0, 0);
		JLabel end = newLabel("Partita Conclusa!", GameColors.ARANCIONE_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
		cronometro.stop();
		goAhead = false;
		updateUI();
	}

	public boolean goAhead() {
		return goAhead;
	}

	public void addActionListenerToGame(ActionListener al) {
		currentActionListener = al;
		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 0; j < WIDTH; j++) {
				tableView[i][j].addActionListener(al);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		timeMax--;
		gameClock.setText("0:" + timeMax);
		if (timeMax == 0)
			gameEnded();
	}

	public JButton[][] getTableView() {
		return tableView;
	}

	public void startTimer() {
		cronometro.start();

	}

	public void addKeyListenerToGame(KeyListener kl) {
		main.addKeyListener(kl);

	}

	public void setAwake(int i, int j) {
		tableView[i][j].setIcon(awakeMole);

	}

}
